﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

namespace Xin.RenRen.PhotoSetDownloader
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            this.Title = Xin.RenRen.PhotoSetDownloader.Properties.Resources.AppName;
            this.Closing += new System.ComponentModel.CancelEventHandler(Window1_Closing);
        }

        void Window1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void btnSaveAll_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtUserName.Text.Trim()))
            {
                MessageBox.Show("请输入人人网用户名");
                return;
            }
            if (string.IsNullOrEmpty(txtPassword.Password))
            {
                MessageBox.Show("请输入密码");
                return;
            }
            if (string.IsNullOrEmpty(txtSetUrl.Text.Trim()))
            {
                MessageBox.Show("请相册页面网址");
                return;
            }
            var rr = new RenRen(txtUserName.Text.Trim(), txtPassword.Password.Trim());
            rr.TraceInfo += (info) =>
                {
                    Dispatcher.Invoke((Action)delegate
                    {
                        var newItem = listStatus.Items.Count + ":" + info;
                        Trace(newItem);
                    });
                };
            rr.Progress += (s, args) =>
                {
                    Dispatcher.Invoke((Action)delegate
                    {
                        progressBar1.Maximum = args.Total;
                        progressBar1.Value = args.Downloaded;
                    });
                };
            rr.PageProgress += (s, args) =>
                {
                    Dispatcher.Invoke((Action)delegate
                    {
                        if (args.Total == args.Downloaded)
                        {
                            progressEffect.Visibility = Visibility.Collapsed;
                            progressBar2.Maximum = args.Total;
                            progressBar2.Value = args.Downloaded;
                            Trace("下载相册完成！如过有下载失败的图片，请尝试重新下载");
                        }
                        else
                        {
                            progressBar2.Maximum = args.Total;
                            progressBar2.Value = args.Downloaded;
                        }
                    });
                };
            rr.DownloadPhotoSet(txtSetUrl.Text.Trim(), txtSaveDir.Text.Trim());
            progressEffect.Visibility = Visibility.Visible;
        }

        private void Trace(string newItem)
        {
            listStatus.Items.Add(newItem);
            listStatus.ScrollIntoView(newItem);
        }
    }
}
